<?php
if(!defined('IN_KKFRAME')) exit();
if(!$uid) showmessage('请先登陆后再发帖', 'member.php?do=login');
$subject = htmlspecialchars(trim(daddslashes($_POST['subject'])));
$contents = htmlspecialchars(trim(daddslashes($_POST['contents'])));
$fid = intval($_POST['fid']);
if(!$subject || !$contents) showmessage('信息不完整，无法提交！', referer());
if(strlen($subject) >= 80) showmessage('标题太长啦，会被怀疑刷屏的噢~', referer());
if(strlen($contents) >= 2500) showmessage('我的天啊，你是自己发的帖子么？怎么这么长啊…', referer());
if(!$_K['config']['forums'][$fid]) showmessage('帖子分类不存在，请返回检查', referer());

if(!$_K['member']['adminid']){
	$user = DB::fetch_first("SELECT * FROM user WHERE uid='{$_K[uid]}'");
	if(!$user){
		dsetcookie('auth');
		showmessage('您的账号已被删除，无法回复', referer());
	}
	if($user['ban']) showmessage('您已经被禁止回复', referer());
	$last_reply = DB::result_first("SELECT MAX(time) FROM post WHERE author='{$_K[uid]}'");
	if($last_reply > TIMESTAMP - 15) showmessage('您回复太频繁了，请稍后再发', referer());
}

$tid = DB::insert('thread', array(
	'fid'			=> $fid,
	'author'		=> $_K['uid'],
	'author_name'	=> addslashes($_K['member']['username']),
	'subject'		=> $subject,
	'desc'			=> cutstr($subject, 140),
	'time'			=> TIMESTAMP,
	'lastpost'		=> TIMESTAMP,
));

DB::insert('post', array(
	'tid'			=> $tid,
	'content'		=> $contents,
	'author'		=> $_K['uid'],
	'author_name'	=> addslashes($_K['member']['username']),
	'time'			=> TIMESTAMP,
));

showmessage('发帖成功！', "thread.php?id={$tid}", 0);